package com.lyh.hkcloud.base;

import android.util.Log;
import android.view.View;
import android.widget.*;
import com.lyh.hkcloud.R;
import com.lyh.hkcloud.module.common.WmsModuleService;
import com.lyh.hkcloud.util.base.*;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class WmsModuleActivity<P extends BaseEntityViewAdapter> extends ModuleActivity<P> {

    private WmsModuleService service;

    protected Spinner spWhid;

    protected EditText etBoxId;

    protected EditText etLocationId;

    @Override
    protected void onBeginInit() {
        service = getService();
    }

    @Override
    protected View getLoadFocusView() {
        return etBoxId;
    }

    @Override
    protected int getClearResId() {
        return R.id.btn_clear;
    }

    @Override
    protected int getSubmitResId() {
        return R.id.btn_submit;
    }

    @Override
    protected int getNowDateResId() {
        return 0;//R.id.tv_nowDate;
    }

    protected boolean enableWhid() {
        return true;
    }

    protected boolean enableLocationId() {
        return true;
    }

    protected boolean enableBoxId() {
        return true;
    }


    protected boolean initEventSoftInputOnFocus(EditText et) {
        return true;
    }

    @Override
    protected void initView() {
        spWhid = findViewById(R.id.sp_whid);
        etBoxId = findViewById(R.id.et_boxId);
        etLocationId = findViewById(R.id.et_locationId);
        if (!enableWhid()) {
            ((LinearLayout) spWhid.getParent()).setVisibility(View.GONE);
        }
        if (!enableLocationId()) {
            ((LinearLayout) etLocationId.getParent()).setVisibility(View.GONE);
        }
        if (!enableBoxId()) {
            ((LinearLayout) etBoxId.getParent()).setVisibility(View.GONE);
        }
    }

    @Override
    protected void initEvent() {
        ViewUtil.setShowSoftInputOnFocus(rootView, new OnEnterEventListener() {
            @Override
            public boolean onEnter(final EditText et) {
                switch (et.getId()) {
                    case R.id.et_boxId:
                        service.checkBox(et.getText().toString(), new JsonSuccessListener() {
                            @Override
                            public void onRespone(String msg, JSONObject response) {
                                boxCallback(JsonUtil.getMap(response, "title"), JsonUtil.getList(response, "data"));
                            }
                        }, new JsonFailuredListener() {
                            @Override
                            public void onRespone(String msg, JSONObject response) {
                                Log.e("error",response.optString("message"));
                                et.setText("");
                                boxCallback(new HashMap<String, Object>(), new ArrayList<Map<String, Object>>());
                                et.requestFocus();
                            }
                        });
                        return true;
                    case R.id.et_locationId:
                        service.checkLocation(et, new JsonSuccessListener() {
                            @Override
                            public void onRespone(String msg, JSONObject response) {
                                locationCallback();
                            }
                        }, new JsonFailuredListener() {
                            @Override
                            public void onRespone(String msg, JSONObject response) {
                                Log.e("error",response.optString("message"));
                                et.setText("");
                                boxCallback(new HashMap<String, Object>(), new ArrayList<Map<String, Object>>());
                                et.requestFocus();
                            }
                        });
                        return true;
                    default:
                        return initEventSoftInputOnFocus(et);
                }
            }
        });
        if (enableWhid()) {
            spWhid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    onClear();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }
    }

    protected void boxCallback(Map<String, Object> title, List<Map<String, Object>> data) {
        if (enableLocationId()) {
            etLocationId.requestFocus();
        } else if (tvCode != null) {
            tvCode.requestFocus();
        }
    }

    protected void locationCallback() {
        if (tvCode != null) {
            tvCode.requestFocus();
        }
    }

    @Override
    protected void initData() {
        if (enableWhid()) {
            LoadingUtils.getInstance(getActivity()).show();
            whids.clear();
            service.getWhids(new JsonSuccessListener() {
                @Override
                public void onRespone(String msg, JSONObject response) {
                    List<Map<String, Object>> ll = JsonUtil.getList(response, "data");
                    for (Map<String, Object> m : ll) {
                        whids.add(MapUtil.getString(m, "whid"));
                    }
                    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, whids);
                    //设置下拉列表的风格
                    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
                    spWhid.setAdapter(spinnerAdapter);
                    LoadingUtils.getInstance(getActivity()).hide();
                }
            }, new JsonFailuredListener() {
                @Override
                public void onRespone(String msg, JSONObject response) {
                    Log.e("error",response.optString("message"));
                    LoadingUtils.getInstance(getActivity()).hide();
                }
            });
        }
    }

    protected List<String> whids = new ArrayList<>();

    protected void initWmsResponse(JSONObject response) {
        JsonUtil.put(response, "postDate", tvNowDate.getText().toString());
        if (enableWhid()) {
            JsonUtil.put(response, "whid", whids.get(spWhid.getSelectedItemPosition()));
        }
        if (enableBoxId()) {
            JsonUtil.put(response, "boxId", etBoxId.getText().toString().trim());
        }
        if (enableLocationId()) {
            JsonUtil.put(response, "locationId", etLocationId.getText().toString().trim());
        }
    }

    @Override
    protected boolean onValidate() {
        if (!super.onValidate()) {
            return false;
        }
        if (enableWhid() && whids.isEmpty()) {
            ToastUtils.makeText(this, "当前操作人没有绑定仓库！", ToastUtils.LENGTH_SHORT).show();
            return false;
        }
        if (enableLocationId() && StringUtils.isEmpty(etLocationId.getText().toString())) {
            ToastUtils.makeText(this, "区域必填", ToastUtils.LENGTH_SHORT).show();
            etLocationId.requestFocus();
            return false;
        }
        if (enableBoxId() && StringUtils.isEmpty(etBoxId.getText().toString())) {
            ToastUtils.makeText(this, "箱子号必填", ToastUtils.LENGTH_SHORT).show();
            etBoxId.requestFocus();
            return false;
        }
        return true;
    }

    protected abstract WmsModuleService getService();

    protected final String getWhid() {
        if (spWhid != null && whids != null && whids.size() > 0 && spWhid.getSelectedItemPosition() > -1) {
            return whids.get(spWhid.getSelectedItemPosition());
        }
        return null;
    }

    protected final String getLocationId() {
        return etLocationId.getText().toString().trim();
    }

    protected final String getBoxId() {
        return etBoxId.getText().toString().trim();
    }
}
